package com.kaiwukj.android.ufamily.mvp.ui.page.splash

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.provider.Settings
import android.view.WindowManager
import cn.anline.permission.AnnPermission
import cn.anline.permission.PermissionCallback
import cn.anline.permission.PermissionItem
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.kaiwukj.android.ufamily.R
import com.kaiwukj.android.ufamily.app.MyApplication
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity
import com.kaiwukj.android.ufamily.app.constant.CodeLoginRouterUrl
import com.kaiwukj.android.ufamily.app.constant.CommunityUpdateUrl
import com.kaiwukj.android.ufamily.app.constant.MainRouterUrl
import com.kaiwukj.android.ufamily.app.constant.SPParam
import com.kaiwukj.android.ufamily.mvp.helper.AccountStore
import com.kaiwukj.android.ufamily.mvp.helper.TokenStore
import com.kaiwukj.android.ufamily.utils.SPUtils

/**
 * @author Eddie Android Developer
 * @company Q | 樽尚汇
 * @since 2019/2/19
 * first start screen
 */
class BootScreenActivity : BaseActivity() {

    override fun getLayoutId(): Int {
        //window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        return R.layout.activity_boot
    }

    override fun initView(savedInstanceState: Bundle?) {
        requestPermissions()

        val isFirst =
                SPUtils.getInstance(SPParam.APP).getBoolean(SPParam.IS_FIRST, true)

        when {
            isFirst -> {
                MyApplication.getInstance().isFirst = isFirst
                SPUtils.getInstance(SPParam.APP).put(SPParam.IS_FIRST, false)
            }
        }
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

    override fun onBackPressedSupport() {
    }


    private fun requestPermissions() {
        unSubscribe()
        /*val rxPermission = RxPermissions(this)
        subscribe(rxPermission
                .request(
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CALL_PHONE
                ).subscribe { granted ->
                    //LogUtils.d("isGranted----------------->$granted")
                    when {
                        granted -> toNext()
                        else -> openSettingActivity(this,
                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                "开启权限才能使用")
                    }
                })*/

        val permissionItem: MutableList<PermissionItem> = ArrayList()
        permissionItem.add(PermissionItem(Manifest.permission.CAMERA,
                getString(R.string.permission_camera), R.drawable.permission_ic_camera))
        permissionItem.add(PermissionItem(Manifest.permission.READ_PHONE_STATE,
                "设备信息", R.drawable.permission_ic_phone))
        permissionItem.add(PermissionItem(Manifest.permission.ACCESS_FINE_LOCATION,
                getString(R.string.permission_location), R.drawable.permission_ic_location))
        permissionItem.add(PermissionItem(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                getString(R.string.permission_storage), R.drawable.permission_ic_storage))
        permissionItem.add(PermissionItem(Manifest.permission.RECORD_AUDIO,
                "录音", R.drawable.permission_ic_sensors))
        AnnPermission.create(this)
                .permissions(permissionItem)
                .style(R.style.dialog_permission)
                .checkMutiPermission(object : PermissionCallback {
                    override fun onFinish() {
                        toNext()
                    }

                    override fun onDeny(permission: String?, position: Int) {
                        openSettingActivity(this@BootScreenActivity,
                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                                "开启权限才能使用")
                    }

                    override fun onGuarantee(permission: String?, position: Int) {
                    }

                    override fun onClose() {
                        finish()
                    }
                })
    }

    private fun toNext() {
        LogUtils.d("---------toNext---------")
        Handler().postDelayed({
            if (!TokenStore.isLogin() || !AccountStore.isLogin()) {
                // fastLogin()
                ARouter.getInstance().build(CodeLoginRouterUrl).navigation(this, navigationCallback)
            } else {
                /*val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)*/
                if (AccountStore.getUserResult().lastCommunityId < 0)
                    ARouter.getInstance().build(CommunityUpdateUrl).navigation(this, navigationCallback)
                else
                    ARouter.getInstance().build(MainRouterUrl).navigation(this, navigationCallback)
            }
            //this@BootScreenActivity.finish()
        }, 2000)
        //TODO 判断登陆状态x
    }

    private val navigationCallback = object : NavigationCallback {
        override fun onLost(postcard: Postcard?) {
        }

        override fun onFound(postcard: Postcard?) {
        }

        override fun onInterrupt(postcard: Postcard?) {
        }

        override fun onArrival(postcard: Postcard?) {
            this@BootScreenActivity.finish()
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_SETTINGS) {
            requestPermissions()
        }
    }

}
